package com.imsprint.xmlldemo.widget


import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.graphics.drawable.AnimationDrawable
import com.imsprint.xmlldemo.R
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshKernel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.constant.SpinnerStyle


class MeiTuanHeader: LinearLayout, RefreshHeader {

    constructor(context: Context): this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr){
        initView(context)
    }


    private var mIVFirst:ImageView? = null
    private var mIVSecond:ImageView? = null
    private var mIVThird:ImageView? = null
    private fun initView(context: Context) {
        val view = View.inflate(context, R.layout.widget_meituan_refresh_header,this)
        mIVFirst = view.findViewById(R.id.ivFirst)
        mIVSecond = view.findViewById(R.id.ivSecond)
        mIVThird = view.findViewById(R.id.ivThird)
    }


    override fun getSpinnerStyle(): SpinnerStyle {
        return SpinnerStyle.Translate  //指定为平移，不能null
    }

    @SuppressLint("RestrictedApi")
    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {

    }

    @SuppressLint("RestrictedApi")
    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {

    }

    @SuppressLint("RestrictedApi")
    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    override fun getView(): View {
        return this
    }

    @SuppressLint("RestrictedApi")
    override fun setPrimaryColors(vararg colors: Int) {

    }



    override fun isSupportHorizontalDrag(): Boolean {
        return false
    }



    @SuppressLint("RestrictedApi")
    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    private var mStatus:Int = 0
    @SuppressLint("RestrictedApi")
    override fun onStateChanged(refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState) {
        when (newState) {
           // 下拉开始刷新
            RefreshState.None, RefreshState.PullDownToRefresh ->{
                mStatus = 0
            }
            //正在刷新
            RefreshState.Refreshing -> {
                mStatus = 2
            }
            //释放立即刷新
            RefreshState.ReleaseToRefresh ->{
                mStatus = 1
            }
            else->{}
        }
    }

    @SuppressLint("RestrictedApi")
    override fun onMoving(isDragging: Boolean, percent: Float, offset: Int, height: Int, maxDragHeight: Int) {
        when(mStatus){
            0->{
                if (isDragging){
                    startFirst()
                }
                if(percent <= 1.2){
                    scaleImage(percent,height)
                }else{
                    scaleImage(1f,height)
                }
            }
            1->{
                if (isDragging && (mSecondAnimationDrawable == null || mSecondAnimationDrawable?.isRunning == false)){
                    startSecondAnimation()
                }else if (!isDragging &&(mThirdAnimationDrawable == null || mThirdAnimationDrawable?.isRunning == false )){
                    startThreeAnimation()
                }
            }
            2->{
                startFirst()
                if(percent <= 1.2){
                    scaleImage(percent,height)
                }else{
                    scaleImage(1f,height)
                }
            }

        }
    }



    private fun scaleImage(scale: Float,height:Int) {
        mIVFirst?.let {
            it.scaleX = scale - 0.2f
            it.scaleY = scale -0.2f
            it.translationY = (height-height*scale)/2f
        }
    }

    @SuppressLint("RestrictedApi")
    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        cancelAnimate()//停止动画
        if (success) {
//            刷新完成

        } else {
//            刷新失败

        }
        //延迟500毫秒之后再弹回
        return 500
    }



    private var mSecondAnimationDrawable: AnimationDrawable? = null
    private var mThirdAnimationDrawable:AnimationDrawable? = null

   private fun startFirst(){
        mIVFirst?.visibility = View.VISIBLE
        mIVSecond?.visibility = View.GONE
        mIVThird?.visibility = View.GONE
    }


  private  fun startSecondAnimation(){
        mIVFirst?.visibility = View.GONE
        mIVSecond?.visibility = View.VISIBLE
        mIVThird?.visibility = View.GONE
        mIVSecond?.setImageResource(R.drawable.pull_to_refresh_second_anim)
        mSecondAnimationDrawable = mIVSecond?.drawable as AnimationDrawable
        mSecondAnimationDrawable?.stop()
        mSecondAnimationDrawable?.start()
    }

    private fun startThreeAnimation(){
        mIVFirst?.visibility = View.GONE
        mIVSecond?.visibility = View.GONE
        mIVThird?.visibility = View.VISIBLE
        mIVThird?.setImageResource(R.drawable.pull_to_refresh_third_anim)
        mThirdAnimationDrawable = mIVThird?.drawable as AnimationDrawable
        mThirdAnimationDrawable?.stop()
        mThirdAnimationDrawable?.start()
    }





    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        cancelAnimate()
    }

    private fun cancelAnimate() {
        if (mSecondAnimationDrawable?.isRunning == true){
            mSecondAnimationDrawable?.stop()
        }
        if (mThirdAnimationDrawable?.isRunning == true){
            mThirdAnimationDrawable?.stop()
        }
    }


}